goog.provide('td.Weapon');

goog.require('Stefan.Rect');
goog.require('Stefan.Circle');
goog.require('td.Bullet');

td.Weapon = T.lang.createClass(function(config){
	var config = config || {};
	this.type = config.type;
	this.grid = config.grid;
	this.pX = this.grid.pX;
	this.pY = this.grid.pY;
	this.map = config.grid.map;
	this.isSelected = false;
	this.cfg = td.Weapon.getCfg(this.type);
	this.fireInterval = this.fireIntervalCache = Math.floor(Math.max(2 / (this.cfg.speed * td.GRIDSIZE), 1)) * 10;
	this.body = td.Weapon.buildBody(this.cfg);
	this.body.info = this.cfg.info;
});

td.Weapon.TYPE = {
	LIGHT: 1,
	MIDDLE: 2,
	HEAVY: 3
};

td.Weapon.masks = {};

td.Weapon.getCfg = function(type){
	switch(type){
		case td.Weapon.TYPE.LIGHT:
			return {
				name: 'Light',
				speed: 0.1,
				bulletSpeed: 0.2,
				damage: 10,
				scope: 4 * td.GRIDSIZE,
				cost: 1,
				url: 'img/cannon1.gif',
				size: td.GRIDSIZE/2,
				gunWidth: 3,
				info: '机关枪\n射程4\n价格1\n攻击1\n攻速快'
			};
		case td.Weapon.TYPE.MIDDLE: 
			return {
				name: 'Middle',
				bulletSpeed: 0.2,
				speed: 0.15,
				damage: 50,
				scope: 5 * td.GRIDSIZE,
				cost: 5,
				url: 'img/cannon2.gif',
				size: td.GRIDSIZE * 2/3,
				gunWidth: 5,
				info: '重机枪\n射程5\n价格5\n攻击5\n攻速中'
			};
		case td.Weapon.TYPE.HEAVY:
			return {
				name: 'Heavy',
				bulletSpeed: 0.2,
				speed: 0.2,
				damage: 100,
				scope: 6*td.GRIDSIZE,
				cost: 20,
				url: 'img/cannon3.gif',
				size: td.GRIDSIZE*3/4,
				gunWidth: 7,
				info: '大炮\n射程6\n价格20\n攻击20\n攻速慢'
			};
	}
};
td.Weapon.getMask = function(type){
	if(!type) return;
	if(!td.Weapon.masks[type]){
		var cfg = td.Weapon.getCfg(type);
		var mask = new Stefan.Circle().setSize(cfg.scope, cfg.scope).setFill('rgba(0,0,0,.1)').setAnchor(0.5,0.5);
		mask.first = true;
		var body = this.buildBody(cfg);
		mask.appendChild(body);
		td.Weapon.masks[type] = mask;
	}
	else
		td.Weapon.masks[type].first = false;
	return td.Weapon.masks[type];
};

td.Weapon.buildBody = function(cfg){
	return new Stefan.Rect().setSize(40, 40).setFill(cfg.url).setAnchor(.5, .5);
};

td.Weapon.extend({
	fire: function () {
		if(!this.target || this.target.isDead) return;
		var angle = 360 * Math.atan((this.target.pX - this.pX)/(this.target.pY - this.pY))/(2 * Math.PI);
		angle += this.target.pY >= this.pY? 180: 0;
		
		this.body.setRotation(angle);
		var bullet = new td.Bullet({
			weapon: this,
			damage: this.cfg.damage,
			target: this.target,
			speed: this.cfg.bulletSpeed,
			pX: this.pX,
			pY: this.pY
		});
	},
	
	findTarget: function () {
		// 如果有目标，且目标在射程内
		if (this.target && !this.target.isDead && 
			Math.pow(this.target.pX - this.pX, 2) + Math.pow(this.target.pY - this.pY, 2) <= Math.pow(this.cfg.scope/2, 2))
			return;
	
		// 重新寻找射程之内的目标
		var armyArr = this.map.armyArr;
		this.target = null;
		for(var i = 0, maxi = armyArr.length; i < maxi; i++){
			var army = armyArr[i];
			if(army && Math.pow(army.pX - this.pX, 2) + Math.pow(army.pY - this.pY, 2) <= Math.pow(this.cfg.scope/2, 2)){
				this.target = army;
				break;
			}
		}
	},
	
	step: function () {
		if(!--this.fireInterval){
			this.findTarget();
			this.fire();
			this.fireInterval = this.fireIntervalCache;
		}
	}
});
